package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    //
    @Operation(summary = "查询所有分类（1、2、3级分类）")
    @GetMapping("/category/getBaseCategoryList")
    @GuiGuCache(prefix = "baseCategoryList:")
    public Result<List<JSONObject>> getBaseCategoryList() {

        List<JSONObject> jsonObjectList = albumInfoService.getBaseCategoryList();
        return Result.ok(jsonObjectList);
    }

    @GuiGuLogin
    @Operation(summary = "保存专辑")
    @PostMapping("/albumInfo/saveAlbumInfo")
    public Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo) {
        albumInfoService.saveAlbumInfo(albumInfoVo);
        return Result.ok();
    }


   @GuiGuLogin(required = true)
    @Operation(summary = "根据当前当前用户查询专辑分页列表")
    @PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}")
    public Result<Page<AlbumListVo>> findUserAlbumPage(@PathVariable("page") Long page,
                                                       @PathVariable Long limit,
                                                       @RequestBody AlbumInfoQuery albumInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        albumInfoQuery.setUserId(userId);
        Page<AlbumListVo> page1 =new Page(page,limit);

        page1= albumInfoService.findUserAlbumPage(page1,albumInfoQuery);
         return Result.ok(page1);

    }

    @GuiGuLogin
    ///api/album/albumInfo/removeAlbumInfo/{id}
    @Operation(summary = "删除专辑")
    @DeleteMapping("/albumInfo/removeAlbumInfo/{id}")
    public Result removeAlbumInfo(@PathVariable Long id){
        albumInfoService.removeAlbumInfo(id);
        return Result.ok();
    }


    @Operation(summary = "根据id回显数据")
    @GetMapping("/albumInfo/getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfo (@PathVariable Long id){
         AlbumInfo albumInfo= albumInfoService.getAlbumInfo(id);
        return Result.ok(albumInfo);
    }

    @Operation(summary = "修改专辑")
    @PutMapping("/albumInfo/updateAlbumInfo/{id}")
    public Result  updateAlbumInfo(@PathVariable Long id,@RequestBody AlbumInfoVo albumInfoVo){
        albumInfoService.updateAlbumInfo(id,albumInfoVo);
        return Result.ok();
    }

    @Operation(summary = "根据当前用户查询全部专辑列表")
    @GetMapping("albumInfo/findUserAllAlbumList")
    public Result<List<AlbumInfo>> findUserAllAlbumList(){
        Long userId = AuthContextHolder.getUserId();
        List<AlbumInfo> albumInfoList = albumInfoService.findUserAllAlbumList(userId);
        return Result.ok(albumInfoList);
    }


    @Operation(summary = "根据三级分类的id查询1 2 3级信息")
    @GetMapping("/category/getCategoryView/{category3Id}")
    @GuiGuCache(prefix = "category3View:")
    public  Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id){
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
        return Result.ok(baseCategoryView);

    }

    /**
     * 根据一级分类Id查询三级分类列表
     */
    @Operation(summary ="根据一级分类Id查询三级分类列表" )
    @GetMapping("category/findTopBaseCategory3/{category1Id}")
    @GuiGuCache(prefix = "baseCategory3Top7:")
    public Result<List<BaseCategory3>> getTop7BaseCategory3(@PathVariable Long category1Id){
        List<BaseCategory3> list = albumInfoService.getTop7BaseCategory3(category1Id);
        return Result.ok(list);
    }


    /**
     * 根据一级分类id获取全部分类信息
     * @param category1Id 一级分类
     * @return
     */
    @GetMapping("/category/getBaseCategoryList/{category1Id}")
    @Operation(summary = "根据一级分类id获取全部分类信息")
    @GuiGuCache(prefix = "category1Id:attributes:")
    public Result<JSONObject> getBaseCategoryList(@PathVariable Long category1Id){
        JSONObject jsonObject = albumInfoService.getBaseCategoryList002(category1Id);
        return Result.ok(jsonObject);
    }


    @Operation(summary = "根据专辑ID获取专辑统计信息")
    @GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
    public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId){
        return Result.ok(albumInfoService.getAlbumStatVo(albumId));
    }


    ///api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
    @GuiGuLogin(required = false)
    @Operation(summary = "查询专辑声音分页列表")
    @GetMapping("trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId ,@PathVariable Long page,@PathVariable Long limit){
        Page<AlbumTrackListVo> albumListVoPage = new Page<>(page,limit);
        Long userId = AuthContextHolder.getUserId();
        albumListVoPage = albumInfoService.findAlbumTrackPage(albumId, userId, albumListVoPage);
        return Result.ok(albumListVoPage);
    }

    /**
     * 提供给订单服务渲染购买商品（声音）列表-查询当前用户待购买声音列表
     *
     * @param trackId    声音ID
     * @param trackCount 数量
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "提供给订单服务渲染购买商品（声音）列表-查询当前用户待购买声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> getWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable int trackCount) {
        List<TrackInfo> trackInfoList = albumInfoService.getWaitBuyTrackInfoList(trackId,trackCount);
        return Result.ok(trackInfoList);
    }





}

